import 'call_exception.dart';

///封装Future结果
class Result<T> {
  get isSuccess => data != null;

  T? get data {
    if (this is SuccessResult<T>) {
      return (this as SuccessResult<T>).resultData;
    }
    return null;
  }

  // $core.bool? success,
  //     $core.String? code,
  //     $core.String? message,

  successOr(T value) {
    return data ?? value;
  }

  int get errorCode {
    if (this is ErrorResult<T>) {
      var exception = (this as ErrorResult<T>).exception;
      if (exception is CallException) {
        return exception.code;
      }
    }
    return 0;
  }

  String? get errorMsg {
    if (this is ErrorResult<T>) {
      var exception = (this as ErrorResult<T>).exception;
      if (exception is CallException) {
        return exception.msg;
      }
      return null;
    }
    return null;
  }
}

class LoadingResult extends Result<void> {}

class ErrorResult<T> extends Result<T> {
  Exception exception;

  ErrorResult(this.exception);

  @override
  String toString() {
    return 'ErrorResult{exception: $exception}';
  }
}

class SuccessResult<T> extends Result<T> {
  T resultData;

  SuccessResult(this.resultData);
}
